﻿function extraInit()
{
    var nameCells = $(".user .name-cell");
    var maxWidth = 30;
    for (var i=0; i<nameCells.length; i++)
    {
        var cellWidth = nameCells.eq(i).width();
        if (cellWidth > maxWidth)
        {
            maxWidth = cellWidth;
        }
    }
    for (var i=0; i<nameCells.length; i++)
    {
        nameCells.eq(i).css("width", maxWidth);
    }
}

function addUserToRole(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var nameInput = bar.find("input[name=name]");
    var argsVariable = new Object();
    var answer = window.showModalDialog("/User/AddUserToRole.aspx?userId="
        +idInput.val() +"&userName="+nameInput.val(),
        argsVariable, 
        "dialogWidth:300px; dialogHeight:200px; dialogLeft:250px; dialogTop:200px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
}

function removeRoleFromUser(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var nameInput = bar.find("input[name=roleName]");
    var argsVariable = new Object();
    var answer = window.showModalDialog("/User/RemoveRoleFromUser.aspx?userId="
        +idInput.val() +"&roleName="+nameInput.val(),
        argsVariable, 
        "dialogWidth:300px; dialogHeight:200px; dialogLeft:250px; dialogTop:200px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
}

function viewRolesInDepartment(button)
{
    var line = $(button.parentNode);
    var idInput = line.find("input[name=id]");
    window.location.href = "/Role/RoleInDepartment.aspx?departmentId="+idInput.val();
}

function removeRoleFromDepartment(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=hdfRoleId]");
    var deptIdInput = $("#refreshForm").find("input[name=hdfDepartmentId]");
    var argsVariable = new Object();
    var answer = window.showModalDialog("/Role/RemoveRoleFromDepartment.aspx?roleId="+idInput.val() + "&departmentId=" + deptIdInput.val(),
        argsVariable, 
        "dialogWidth:300px; dialogHeight:200px; dialogLeft:250px; dialogTop:200px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
}

function addRole()
{
    var src = "/Role/AddRole.aspx";
    $("#detailFrame").attr("src", src);
}

function viewRole(node)
{
    var bar = $(node.parentNode).next();
    var idInput = bar.find("input[name=id]");
    var src = "/Role/ViewRole.aspx?id="+idInput.val();
    $("#detailFrame").attr("src", src);
}

function editRole(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var src = "/Role/EditRole.aspx?id="+idInput.val();
    $("#detailFrame").attr("src", src);
}

function removeRole(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var src = "/Role/RemoveRole.aspx?id="+idInput.val();
    $("#detailFrame").attr("src", src);
}

function tickAudit(checkbox)
{
    var parent = $(checkbox.parentNode);
    var modes = "";
    var ckbs = parent.find("input[type=checkbox]");
    for (var i=0; i<ckbs.length; i++)
    {
        if (ckbs.eq(i).attr("checked") == "checked")
        {
            if (modes != "")
            {
                modes += "|";
            }
            modes += ckbs.eq(i).val();
        }
    }
    parent.find("input[name=hdfAuditMode]").val(modes);
}


function selectUser(cell)
{
    var jRow = $(cell).closest(".div-row");
    var table = jRow.parent();
    var rows = table.find(".div-row");
    for (var i=0; i<rows.length; i++)
    {
        rows.eq(i).removeClass("selected");
    }
    if (!jRow.hasClass("selected"))
    {
        jRow.addClass("selected");
    }
    
    var userId = jRow.find("input[name=hdfUserId]").val();

    var area = mask();
    $.post(
		"/AjaxUser.aspx", 
		{
		    "userId" : userId, 
		    "action" : "select_user"
		}, 
		function (r) {
		    if (r.status)
		    {
		        var divAudits = $("#divAudits");
		        var ckbs = divAudits.find("input[type=checkbox]");
		        ckbs.removeAttr("checked");
		        for (var i=0; i<r.list.length; i++)
		        {
		            var splits = r.list[i].modes.split("|");
		            for (var j=0; j<splits.length; j++)
		            {
		                var inputName = "ckb_" + r.list[i].audit;
		                divAudits.find("input[name="+inputName+"][value="+splits[j]+"]").attr("checked", "checked");
		            }
		        }
		    }
		}
	)
	.always(function() {
	    unmask(area);
	});
}

function updateAudit()
{
    var jRow = $(".div-row.selected");
    if (jRow.length == 0)
    {
        return;
    }
    var userId = jRow.find("input[name=hdfUserId]").val();
    var cells = $("#divAudits .cell");
    var auditArray = "";
    var modeArray = "";
    for (var i=0; i<cells.length; i++)
    {
        var ckbs = cells.eq(i).find("input[type=checkbox]");
        var audit = null;
        var modes = "";
        for (var j=0; j<ckbs.length; j++)
        {
            if (audit == null)
            {
                var splits = ckbs.eq(j).attr("name").split("_");
                audit = splits[1];
            }
            if (ckbs.eq(j).attr("checked") == "checked")
            {
                if (modes.length > 0)
                {
                    modes += "|";
                }
                modes += ckbs.eq(j).val();
            }
        }
        if (auditArray.length > 0)
        {
            auditArray += ",";
            modeArray += ",";
        }
        auditArray += audit;
        modeArray += modes;
    }

    var area = mask();
    $.post(
		"/AjaxUser.aspx", 
		{
		    "userId" : userId, 
		    "action" : "update_audit",
		    "auditArray" : auditArray,
		    "modeArray" : modeArray
		}, 
		function (r) {
		    if (!r.status)
		    {
		        if (r.error != "")
		        {
		            alert(r.error);
		        }
		    }
		}
	)
	.always(function() {
	    unmask(area);
	});
}

function removeUser(button)
{
    var parent = $(button.parentNode);
    var userId = parent.find("input[name=hdfUserId]").val();
    var area = mask();
    $.post(
		"/AjaxUser.aspx", 
		{
		    "userId" : userId, 
		    "action" : "remove_user"
		}, 
		function (r) {
		    if (!r.status)
		    {
		        if (r.error != "")
		        {
		            alert(r.error);
		        }
		        else if (r.message != "")
		        {
		            alert(r.message);
		        }
		    }
		    else
		    {
		        var row = $(button).closest(".div-row");
		        var table = row.parent();
                var rows = table.find(".div-row");
                for (var i=0; i<rows.length; i++)
                {
                    rows.eq(i).removeClass("selected");
                }
		        row.remove();
		        emptyAuditTable();
		        if (r.message != "")
		        {
		            alert(r.message);
		        }
		    }
		}
	)
	.always(function() {
	    unmask(area);
	});
}

function emptyAuditTable()
{
    var divAudits = $("#divAudits");
    var ckbs = divAudits.find("input[type=checkbox]");
    ckbs.removeAttr("checked");
}